package com.best.demo.controller;

import com.best.demo.model.User;
import com.best.demo.service.LoginService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by b3st9u on 16/8/19.
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "login.action")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView model = new ModelAndView();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            if (!Strings.isNullOrEmpty(username) && !Strings.isNullOrEmpty(password)) {
                User user = this.loginService.login(username, password);
                if (user != null) {
                    request.getSession().setAttribute("login_status", user);
                    model.setViewName("redirect:mainframe.action");
                    return model;
                }
            }

        model.setViewName("login");
        return model;
    }
}
